package org.wisdom.netty.websocket;

import com.google.protobuf.MessageLite;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.EmptyByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import org.wisdom.netty.global.ChannelSupervise;
import org.wisdom.netty.proto.MixRequest;
import org.wisdom.netty.proto.MixResponse;

import io.netty.channel.Channel;
import org.wisdom.netty.proto.SC_IM_MSG_Rsp;
import org.wisdom.netty.proto.SC_Sign_Up_Rsp;

import java.util.Random;

public class ProtoHandler {

    static Random random = new Random();

    // 返回【谁发的发给谁】
    // ctx.channel().writeAndFlush(tws);
    public static void handler(BinaryWebSocketFrame frame, Channel channel) {
        //MixRequest.parseFrom(frame.data.nioBuffer());
        try {
            //PooledUnsafeDirectByteBuf(ridx: 0, widx: 61, cap: 61)
            //java.nio.DirectByteBuffer[pos=0 lim=61 cap=61]
            MixRequest request = MixRequest.parseFrom(frame.content().nioBuffer());
            System.out.println("request:" + request);
            if (request.hasSignUp()) {
                System.out.println("处理登录");
                MixResponse response = MixResponse
                        .newBuilder()
                        .setUId(request.getUId())
                        .setSignUp(SC_Sign_Up_Rsp.getDefaultInstance())
                        .build();

                sendResponse(channel, response);
            } else if (request.hasHBeat()) {
                //处理心跳消息
                System.out.println("处理心跳");
                //sendResponse(MixResponse.getDefaultInstance());
            } else if (request.hasIMMsg()) {
                System.out.println("处理MI消息");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {

                            Thread.sleep(random.nextInt(10) * 1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }

                        MixResponse response = MixResponse
                                .newBuilder()
                                .setUId(request.getUId())
                                .setIMMsg(SC_IM_MSG_Rsp.newBuilder().setMsg(
                                        "server to client :" + System.currentTimeMillis()
                                ).build())
                                .build();

                        sendResponse(channel, response);
                    }
                }).start();


                MixResponse response = MixResponse
                        .newBuilder()
                        .setUId(request.getUId())
                        .setIMMsg(SC_IM_MSG_Rsp.newBuilder().setMsg(
                                "server to client :" + System.currentTimeMillis()
                        ).build())
                        .build();

                sendResponse(channel, response);

            }else if(request.hasLogout()){
                System.out.println("处理Logout消息");

                channel.close();
            } else {
                sendResponse(channel, MixResponse.getDefaultInstance());
            }

        } catch (Exception e) {
            System.out.println("ProtoHandler handler:" + e);
        }
    }

    private static void sendResponse(Channel channel, MixResponse response) {
        ByteBuf byteBuf = Unpooled.wrappedBuffer(response.toByteArray());
        BinaryWebSocketFrame frame = new BinaryWebSocketFrame(byteBuf);
        //ChannelSupervise.send2All(frame);
        channel.writeAndFlush(frame);
    }


}
